 /*
 * UMLSynch - VPSDE plugin part
 * http://umlsynch.googlecode.com
 *
 * Copyright 2008-2010, Evgeny Alexeyev
 * Licensed under the EPL - v1.0
 * http://www.eclipse.org/legal/epl-v10.html
 *
 */

package com.googlecode.umlsynch.vpsde.rpc.VPSDEDiagramManager;

import com.vp.plugin.ApplicationManager;
import com.vp.plugin.diagram.IDiagramElement;
import com.vp.plugin.model.IModelElement;

public class VPPropertyManager {
	private static VPPropertyManager Singletone = null;
	public static VPPropertyManager instance() {
		if (Singletone == null) {
			Singletone = new VPPropertyManager();
		}
		return Singletone;
	}
	
	public void SetProperty(IModelElement element, String prop, String value) {
		if ((element == null)
				|| (prop == null) || (value == null)) {
			return;
		}
		
		String docs = "";
		boolean modifiedProp = false;
		if (element.getDocumentation() != null) {
			String[] properties = element.getDocumentation().split(":");
			for (int i=0; i<properties.length; ++i) {
				String[] propValue = properties[i].split("=");
				if ((propValue != null) && (propValue.length==2) && 
				(propValue[0].equals(prop))) {
					modifiedProp = true;
					docs += prop + "=" + value + ":";
				} else {
					docs+= properties[i];

				}
				if ( i != properties.length) {
					docs+= ":";
				}
			}
			if (!modifiedProp) {
				docs += ":" + prop + "=" + value;
			}
		} else {
			docs = prop + "=" + value;
		}
		element.setDocumentation(docs);		
	}

	public String GetProperty(IModelElement element, String prop) {
		if ((element == null)
				|| (prop == null)) {
			return null;
		}
		String eldoc = element.getDocumentation();
		if (eldoc != null) {
			String[] properties = eldoc.split(":");
			for (int i=0; i<properties.length; ++i) {

				String[] propValue = properties[i].split("=");
				ApplicationManager.instance().getViewManager()				
				.showMessage("Prop: " + properties[i] + "  Length: " + propValue.length + " Value0:");				
				if ((propValue != null) && (propValue.length == 2) 
						&& (propValue[0].equals(prop))) {
					return new String(propValue[1]);
				}
			}
		}	

		return null;
	}

}
